package me.spring.cloud.components.starter.dds.configuration;

import me.spring.cloud.common.components.dds.DataSourceManager;
import me.spring.cloud.common.components.dds.DynamicDataSource;
import me.spring.cloud.common.components.dds.context.ConfigPropertiesBinder;
import me.spring.cloud.common.components.dds.context.DataSourceRefresher;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * @description: 动态数据库配置
 * @author: luffy
 * @create: 2018-09-18 11:04
 **/
@Configuration
@Import(DataSourceRefresher.class)
public class DdsConfiguration {

  @Bean
  public ConfigPropertiesBinder configPropertiesBinder() {
    return new ConfigPropertiesBinder();
  }

  @Bean
  public DataSourceManager dataSourceManager() {
    return new DataSourceManager();
  }

  @Bean
  public DynamicDataSource dataSource(DataSourceManager dataSourceManager) throws SQLException {
    DataSource actualDataSource = dataSourceManager.createDataSource();
    return new DynamicDataSource(actualDataSource);
  }

}
